package com.liang.maogouyuyin.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.liang.maogouyuyin.AppConst
import com.liang.maogouyuyin.databinding.ItemInstructionBinding
import com.liang.maogouyuyin.utils.PlayerUtil

class InstructionAdapter(val context: Context): RecyclerView.Adapter<InstructionAdapter.ViewHolder>() {
    class ViewHolder(binding: ItemInstructionBinding): RecyclerView.ViewHolder(binding.root) {
        val itemImage = binding.itemInstructionImage
        val itemText = binding.itemInstructionText
        val itemPlayer = binding.itemInstructionPlayer
    }

    private var isDog = false

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemInstructionBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return 4
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (isDog)
            holder.itemImage.setImageResource(AppConst.instructionImageDog[position])
        else
            holder.itemImage.setImageResource(AppConst.instructionImageCat[position])
        holder.itemText.text = context.getString(AppConst.instructionTextArray[position])
        holder.itemView.setOnClickListener {
            if (isDog)
                PlayerUtil.playAudio("asset:///sound/dog/instruction_index_${position}.mp3")
            else
                PlayerUtil.playAudio("asset:///sound/cat/instruction_index_${position}.mp3")
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setDog(isDog: Boolean) {
        this.isDog = isDog
        notifyDataSetChanged()
    }
}